package com.ceair.flight.refund.service.audit.impl;

import com.ceair.flight.refund.dto.ResponseConst;
import com.ceair.flight.refund.dto.audit.AuditResult;
import com.ceair.flight.refund.dto.audit.AuditResultRes;
import com.ceair.flight.refund.dto.audit.BaseAuditReq;
import com.ceair.flight.refund.dto.audit.FirstAuditReq;
import com.ceair.flight.refund.location.dt.enums.RefundResConst;
import com.ceair.flight.refund.service.audit.IFirstAudit;
import com.ceair.flight.refund.service.audit.IFirstAuditService;
import com.ceair.flight.refund.util.ValidationResult;
import com.ceair.flight.refund.util.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Service("firstAudit")
@Path("/firstAudit")
public class FirstAuditImpl implements IFirstAudit {
    private static final Logger LOG = LoggerFactory.getLogger(FirstAuditImpl.class);

    @Autowired
    private IFirstAuditService internalFirstAuditServiceImpl;

    @Override
    @POST
    @Path("/passAudit")
    @Consumes({"application/json"})
    @Produces(MediaType.APPLICATION_JSON)
    public AuditResultRes passAudit(FirstAuditReq auditReq) {
        AuditResultRes res = new AuditResultRes();
        LOG.info("一审通过开始！" + auditReq.getApplyId());
//        System.out.println(auditReq);

        AuditResult auditResult = new AuditResult();
        res.setAuditResult(auditResult);
        try {
            // 参数校验
            if (auditReq == null) {
                res.setResultCode(RefundResConst.FIRST_AUDIT_FAIL_CODE);
                res.setResultMsg(RefundResConst.FIRST_AUDIT_FAIL_MSG + "：参数FirstAuditReq不能为空!");
                return res;
//                throw new ServiceException("004030001001", "参数FirstAuditReq不能为空!");
            }
            ValidationResult validationResult = ValidationUtils.validateEntity(auditReq);
            if (validationResult.isHasErrors()) {
                auditResult.setError(true);
                auditResult.setErrorDesc(validationResult.toString());
                return res;
            }
            internalFirstAuditServiceImpl.doPass(auditReq);
        } catch (Exception e) {
            LOG.error("一审通过失败！" +  + auditReq.getApplyId(), e);
            auditResult.setError(true);
            auditResult.setErrorDesc(e.getMessage());
            return res;
        }
        auditResult.setError(false);
        LOG.info("一审通过结束！" + auditReq.getApplyId());
        if(auditResult.hasError()){
            res.setResultCode(RefundResConst.FIRST_AUDIT_FAIL_CODE);
            res.setResultMsg(RefundResConst.FIRST_AUDIT_FAIL_MSG + auditResult.getErrorDesc());
        }else{
            res.setResultCode("0");
            res.setResultMsg("接口调用成功！");
        }
        return res;
    }

    @Override
    @POST
    @Path("/refuseAudit")
    @Consumes({"application/json"})
    @Produces(MediaType.APPLICATION_JSON)
    public AuditResultRes refuseAudit(BaseAuditReq auditReq) {
        LOG.info("一审拒绝开始！");
        AuditResultRes res = new AuditResultRes();
        AuditResult auditResult = new AuditResult();
        try {
            // 参数校验
            if (auditReq == null) {
                res.setResultCode(RefundResConst.ARG_NOT_EXIST_CODE);
                res.setResultMsg(RefundResConst.ARG_NOT_EXIST_CODE + "参数FirstAuditReq不能为空!");
                return res;
//                throw new Exception("参数FirstAuditReq不能为空!");
            }
            ValidationResult validationResult = ValidationUtils.validateEntity(auditReq);
            if (validationResult.isHasErrors()) {
                auditResult.setError(true);
                auditResult.setErrorDesc(validationResult.toString());
                res.setResultCode(RefundResConst.SECOND_AUDIT_FAIL_CODE);
                res.setResultMsg(RefundResConst.SECOND_AUDIT_FAIL_MSG);
                return res;
            }

            internalFirstAuditServiceImpl.doRefuse(auditReq);
        } catch (Exception e) {
            LOG.info("一审拒绝失败！", e);
            auditResult.setError(true);
            auditResult.setErrorDesc(e.getMessage());
            res.setResultCode(RefundResConst.SECOND_AUDIT_FAIL_CODE);
            res.setResultMsg(RefundResConst.SECOND_AUDIT_FAIL_MSG);
            return res;
        }
        auditResult.setError(false);
        res.setResultCode(ResponseConst.SUCCESS_CODE);
        res.setResultMsg(ResponseConst.SUCCESS_MSG);
        LOG.info("一审拒绝结束！");
        return res;
    }
}
